package com.yoomiito.app.ui.fragment.my;

import android.content.Intent;
import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.yoomiito.app.Constant;
import com.yoomiito.app.adapter.my.MyHistoryIncomeAdapter;
import com.yoomiito.app.model.my.MyHistoryIncomeBean;
import com.yoomiito.app.model.my.MyHistoryIncomeInfo;
import com.yoomiito.app.present.my.fragment.MyHistoryIncomeFmPresenter;
import com.yoomiito.app.ui.my.MyHistoryIncomeActivity;
import com.yoomiito.app.ui.my.MyHistoryMonthActivity;
import com.yoomiito.app.widget.MyItemDecoration;

import java.util.ArrayList;
import java.util.List;

import cn.droidlover.xdroidmvp.net.ApiException;

/**
 * 历史总收入页面---fm
 */
public class MyHistoryIncomeFragment extends MyOrderBaseFragment<MyHistoryIncomeFmPresenter> {
    //    @BindView(R.id.rv_order)
//    RecyclerView mRecyclerView;
    private MyHistoryIncomeAdapter mAdapter;
    private List<MyHistoryIncomeBean> mList;

    @Override
    public MyHistoryIncomeFmPresenter newP() {
        return new MyHistoryIncomeFmPresenter();
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        mSmartRefreshLayout.setEnableRefresh(false);
        mSmartRefreshLayout.setEnableLoadMore(false);
        mList = new ArrayList<>();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        mRecyclerView.addItemDecoration(new MyItemDecoration(context, RecyclerView.VERTICAL));
        mRecyclerView.setLayoutManager(linearLayoutManager);

        mAdapter = new MyHistoryIncomeAdapter(mList);
        mRecyclerView.setAdapter(mAdapter);
        getNewData();
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            List<MyHistoryIncomeBean> data = mAdapter.getData();
            int month = data.get(position).getMonth();
            if (month > 0) {
                Intent intent = new Intent(getActivity(), MyHistoryMonthActivity.class);
                intent.putExtra(Constant.KEY_MONTH, data.get(position).getCreate_date_month());
                intent.putExtra(Constant.KEY_MONTH_DATA, month);
                startActivity(intent);
            }
        });
    }

    @Override
    public void handleError(ApiException e) {

    }

    public void setHistoryOrderData(MyHistoryIncomeInfo data) {
        ((MyHistoryIncomeActivity) getActivity()).setData(data);
        if (currentPage == 1) {
            if (data == null || data.getData() == null || data.getData().size() == 0) {
                mStatusManager.showEmpty();
            } else {
                mAdapter.setNewData(transformData(data));
                mSmartRefreshLayout.finishRefresh(true);
                mStatusManager.showContent();
            }
        } else {
            mSmartRefreshLayout.finishLoadMore(true);
            if (data.getData() == null || data.getData().size() == 0) {
                mSmartRefreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                mAdapter.addData(transformData(data));
            }
        }
    }

    private List<MyHistoryIncomeBean> transformData(MyHistoryIncomeInfo data) {
        List<MyHistoryIncomeBean> tempList = new ArrayList<>();
        if (data != null && data.getData() != null) {
            for (MyHistoryIncomeInfo.YearDataBean item : data.getData()) {
                MyHistoryIncomeBean titleHistoryBean = new MyHistoryIncomeBean();
                titleHistoryBean.setTitle(true);
                titleHistoryBean.setYear(item.getYear());
                tempList.add(titleHistoryBean);
                if (item.getData() != null) {
                    tempList.addAll(item.getData());
                }
            }
        }
        return tempList;
    }

    @Override
    protected boolean useMyItemDecoration() {
        return true;
    }

    @Override
    protected void getNewData() {
        getP().getHistoryOrderData(currentPage);
    }

    @Override
    public String getEmptyContent() {
        return "未购买商品";
    }
}
